$(function () {
	var selfUrl=location.href;
	var paramData={};
	if(selfUrl.indexOf("?companyId=")>0){
		var paramUrl=selfUrl.substr(selfUrl.indexOf("?companyId="));
		var paramsList=paramUrl.substr(1).split("&");
		var companyInfoId="";
		var positionId="";
		var positionName="";
		for (var i=0;i<paramsList.length;i++) {
			var parmVar=paramsList[i].split("=");
			if(parmVar[1]&&parmVar[1]!="undefined"){
				if(parmVar[0]=='companyId'){
					companyInfoId=parmVar[1];
				}else if(parmVar[0]=='positionId'){
					positionId=parmVar[1];
				}else if(parmVar[0]=='positionName'){
					positionName=decodeURI(parmVar[1]);
				}
			}
		}
		
		vm.companyIdVal=companyInfoId;
		paramData={'companyId':companyInfoId,'name': vm.q.name};
		if(positionName&&"null"!=positionName){
			vm.q.name=positionName;
		}
		if(positionId&&"null"!=positionId){
			vm.selectPositionId=positionId;
		}
	}
	
    $("#positionDictGrid").Grid({
        url: '../org/positiondict/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '所属公司', name: 'companyName', index: 'company_id', width: 80},
			{label: '职位归属', name: 'codeName', index: 'code', width: 80},
			{label: '职位归属', name: 'code', index: 'code', width: 80, hidden: true},
			{label: '职位名称', name: 'name', index: 'name', width: 80},
			{label: '职位描述', name: 'content', index: 'content', width: 80}],
		postData:paramData,
		gridComplete: function() {
			if(vm.selectPositionId){
			    var rowIds = $("#positionDictGrid").jqGrid('getDataIDs');
			    for(var k=0; k<rowIds.length; k++) {
			       if(rowIds[k]&&rowIds[k]==vm.selectPositionId){
			    	   $("#positionDictGrid").setSelection(rowIds[k], false);
			    	   break;
			       }
			    }
				vm.selectPositionId="";
			}
		},
		beforeSelectRow: function(){
			if(vm.companyIdVal){
				$("#positionDictGrid").jqGrid('resetSelection');
			}
	        return(true);
		}
    });
    
    vm.getPositionSysDictList();
});

var vm = new Vue({
	el: '#positionDict',
	data: {
        showList: true,
        title: null,
		orgPositionDict: {},
		dictList: [],
		selectPositionId:"",
		companyIdVal:'',
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		selectDict:function(val){
		},
		getPositionSysDictList:function(){
			Ajax.request({
				url: "../org/positiondict/dict",
				async: true,
				successCallback: function (r) {
					vm.dictList=r.list;
				}
			});
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.orgPositionDict = {companyId:vm.companyIdVal,codeName:null};
		},
		update: function (event) {
            var id = getSelectedRow("#positionDictGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.orgPositionDict.id == null ? "../org/positiondict/save" : "../org/positiondict/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.orgPositionDict),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#positionDictGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../org/positiondict/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../org/positiondict/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.orgPositionDict = r.orgPositionDict;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#positionDictGrid").jqGrid('getGridParam', 'page');
			$("#positionDictGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'companyId':vm.companyIdVal},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});